﻿<?php
	require_once('conn.php');

	$name=$_POST['name_customer'];
	$sex=$_POST['sex_customer'];
	$id_customer=$_POST['id_customer'];
	$mobileNO=$_POST['searchMobileNO'];
	$score_available=$_POST['score_available'];
	$score=$_POST['score_lose'];
	$gift=$_POST['gift_get'];
	$changeDate=date("Y-m-d");
	$shopID=$_POST['shopID'];
	$optID=$_POST['optID'];
	
	if ($score<100 or $score%100 !=0)
	{
		echo '<script language="JavaScript">window.alert("兑换积分须为100的整数倍，并大于0");</script>'; 
		echo '<script language="JavaScript">window.location.href="serchCustomerInfoResult.php?no='.$mobileNO.'";</script>'; 
	}
	elseif ($score > $score_available)
	{
		echo '<script language="JavaScript">window.alert("兑换积分不可大于可用积分");</script>'; 
		echo '<script language="JavaScript">window.location.href="serchCustomerInfoResult.php?no='.$mobileNO.'";</script>'; 
	}		
	else
	{	
		$exec="INSERT INTO giftRecord(id_customer, mobileNO, score, gift, changeDate, shopID, optID) values('$id_customer', '$mobileNO', '$score', '$gift', '$changeDate', '$shopID', '$optID')";
		if ($result = mysql_query($exec))
		{
			echo '<script language="JavaScript">window.alert("兑换成功");</script>'; 
			$url_header_dd="http://wt.chinaweimei.com.cn/apihttp/SMSSend.aspx?user=%B6%A5%B5%E7%D1%DB%BE%B5%D0%D0&pass=734yvu";
			$url_header_xz="http://wt.chinaweimei.com.cn/apihttp/SMSSend.aspx?user=%C9%EE%DB%DA%D6%EC%CE%B0%C9%FA&pass=weisheng";		
			$score_left=$score_available-$score;
			$msg=$name.$sex."，恭喜您使用".$score."积分成功兑换".$gift."。当前积分可用积分:".$score_left."分";
			$msg=mb_convert_encoding($msg, 'gb2312' ,'utf-8');
			$url=$url_header_dd."&context=".$msg."&mobile=".$mobileNO;
			// var_dump($url);
			file_get_contents($url);		
			echo '<script language="JavaScript">window.location.href="serchCustomerInfoResult.php?no='.$mobileNO.'";</script>'; 	
		}
		else
		{
			echo '<script language="JavaScript">window.alert("兑换失败");</script>'; 
			echo '<script language="JavaScript">window.location.href="serchCustomerInfoResult.php?no='.$mobileNO.'";</script>'; 		
		}
	}
?>